import 'package:flutter_flux/flutter_flux.dart';
import 'package:freemusic/model/AbstractSong.dart';
import 'package:freemusic/model/KugouSong.dart';
import 'package:freemusic/model/LocalSong.dart';
import 'package:freemusic/model/NeteaseSong.dart';
import 'package:freemusic/model/QQSong.dart';
import 'package:freemusic/model/migu/MiguSong.dart';

class SongStore extends Store {
  List<KugouSong> _kugous = List();
  List<QQSong> _qqs = List();
  List<NeteaseSong> _neteases = List();
  List<LocalSong> _localSongs = List();
  List<MiguSong> _migus = List();
  List<String> _keywords = List();
  String _keyword ="";

  List<AbstractSong> _recentSong = List();

  SongStore(){
    triggerOnAction(changeQQList, (List<QQSong> qqs){
      _qqs = qqs;
    });
    triggerOnAction(changeNeteaseList, (List<NeteaseSong> neteases){
      _neteases = neteases;
    });
    triggerOnAction(changeKugouList, (List<KugouSong> kugous){
      _kugous = kugous;
    });
    triggerOnAction(appendQQList, (List<QQSong> qqs){
      _qqs.addAll(qqs);
    });
    triggerOnAction(appendNeteaseList, (List<NeteaseSong> neteases){
      _neteases.addAll(neteases);
    });
    triggerOnAction(appendKugouList, (List<KugouSong> kugous){
      _kugous.addAll(kugous);
    });
    triggerOnAction(putKeyword, (String keyword){
      _keyword = keyword;

      if(_keywords.length > 10){
        _keywords.removeAt(0);
      }
      int index = _keywords.indexOf(keyword);
      if(index >= 0){
        return keyword;
      }
      _keywords.add(keyword);
      return keyword;
    });
    triggerOnAction(putKeywords, (List<String> keywords){
      _keywords = keywords;
    });
    triggerOnAction(changeRecentSongList, (List<AbstractSong> songs){
      _recentSong = songs;
    });
    triggerOnAction(appendRecentSong, (AbstractSong song){
      int index = _recentSong.indexWhere((item) => item.hash == song.hash);
      if(index == -1){
        _recentSong.add(song);
      }
    });
    triggerOnAction(deleteRecentSong, (String hash){
      int index = _recentSong.indexWhere((item) => item.hash == hash);
      if(index != -1){
        _recentSong.removeAt(index);
      }
    });
    triggerOnAction(changeLocalSongList, ( List<LocalSong> songs){
      _localSongs = songs;
    });
    triggerOnAction(deleteLocalSong, (AbstractSong song){
      int index = _localSongs.indexWhere((item) => item.hash == song.hash);
      if(index == -1){
        return ;
      }
      _localSongs.removeAt(index);
    });
    triggerOnAction(changeMiguList, (List<MiguSong> songs){
      this._migus = songs;
    });
    triggerOnAction(appendMiguList, (List<MiguSong> songs){
      this._migus.addAll(songs);
    });
  }

  List<KugouSong> get kugous => _kugous;
  List<QQSong> get qqs => _qqs;
  List<NeteaseSong> get neteases => _neteases;
  List<String> get keywords => _keywords;
  String get keyword => _keyword;
  List<AbstractSong> get recentSong => _recentSong;
  List<LocalSong> get localSongs => _localSongs;
  List<MiguSong> get migus => _migus;
}

final StoreToken songStoreToken = StoreToken(SongStore());

final Action<List<QQSong>> changeQQList = Action();
final Action<List<NeteaseSong>> changeNeteaseList = new Action<List<NeteaseSong>>();
final Action<List<KugouSong>> changeKugouList = new Action<List<KugouSong>>();
final Action<List<AbstractSong>> changeRecentSongList = new Action();
final Action<List<LocalSong>> changeLocalSongList = new Action();
final Action<List<MiguSong>> changeMiguList = new Action();

final Action<List<QQSong>> appendQQList = Action();
final Action<List<NeteaseSong>> appendNeteaseList = new Action<List<NeteaseSong>>();
final Action<List<KugouSong>> appendKugouList = new Action<List<KugouSong>>();
final Action<AbstractSong> appendRecentSong = new Action();
final Action<List<MiguSong>> appendMiguList = new Action();

final Action<String> deleteRecentSong = new Action();
final Action<AbstractSong> deleteLocalSong = new Action();

final Action<String> putKeyword = new Action<String>();
final Action<List<String>> putKeywords = new Action<List<String>>();